package com.ckzp.weixin.wxpay;

import com.ckzp.core.ConfigUtil;
import com.ckzp.core.json.JsonUtil;
import com.ckzp.core.lc.SignTool;
import com.ckzp.core.util.StringUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by 51594 on 2019/11/21.
 */
public class JsApi {

    public static String WX_PAYKEY = ConfigUtil.getProperty("WX_PAYKEY");

    /**
     * 0表示 成功
     * 其失败
     */
    public int code = 0;


    public String appId;
    public int configID = 0;
    public String timeStamp;
    public String nonceStr;
    public String packages;
    public String signType;
    public String paySign;
    public String code_url = "";

    public String errmsg = "";

    public static JsApi n() {
        return new JsApi();
    }

    public int getCode() {
        return code;
    }

    public JsApi setCode(int code) {
        this.code = code;
        return this;
    }

    public String getAppId() {
        return appId;
    }

    public JsApi setAppId(String appId) {
        this.appId = appId;
        return this;
    }

    public String getTimeStamp() {
        return timeStamp;
    }

    public JsApi setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
        return this;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public JsApi setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
        return this;
    }

    public String getPackages() {
        return packages;
    }

    public JsApi setPackages(String packages) {
        this.packages = packages;
        return this;
    }

    public String getSignType() {
        return signType;
    }

    public JsApi setSignType(String signType) {
        this.signType = signType;
        return this;
    }

    public String getPaySign() {
        return paySign;
    }

    public void setPaySign(String paySign) {
        this.paySign = paySign;
    }

    public int getConfigID() {
        return configID;
    }

    public JsApi setConfigID(int configID) {
        this.configID = configID;
        return this;
    }

    public String getCode_url() {
        return code_url;
    }

    public JsApi setCode_url(String code_url) {
        this.code_url = code_url;
        return this;

    }

    public String getErrmsg() {
        return errmsg;
    }

    public JsApi setErrmsg(String errmsg) {
        this.errmsg = errmsg;
        return this;
    }

    public JsApi setPaySign() throws Exception {
        Map m = new TreeMap();
        m.put("appId", this.getAppId());
        m.put("timeStamp", this.getTimeStamp());
        m.put("nonceStr", this.getNonceStr());
        m.put("package", this.getPackages());
        m.put("signType", this.getSignType());
        this.paySign = WXPayUtil.generateSignature(m, getWxPaykey());
        return this;
    }

    public String getWxPaykey() {
        if (configID == 0) {
            return ConfigUtil.getProperty("WX_PAYKEY");
        } else {
            return ConfigUtil.getProperty("WX_PAYKEY_" + configID);
        }
    }

    @Override
    public String toString() {
        return "JsApi{" +
                "code=" + code +
                ", appId='" + appId + '\'' +
                ", timeStamp='" + timeStamp + '\'' +
                ", nonceStr='" + nonceStr + '\'' +
                ", packages='" + packages + '\'' +
                ", signType='" + signType + '\'' +
                ", paySign='" + paySign + '\'' +
                ", code_url='" + code_url + '\'' +
                ", errmsg='" + errmsg + '\'' +
                '}';
    }

    public String toJson() {
        return JsonUtil.toJsonString(this);
    }
}
